package javaWeb;

import Bean.Furn;
import Bean.Page;
import Bean.User;
import Dao.BasicDao;
import Service.CartService;
import Service.FurnService;
import Service.Impl.CartServiceImpl;
import Service.Impl.FurnServiceImpl;
import Utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class CustomerFurnPage extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();
    private Integer MAXPAGESIZE = 4;
   CartService<Object>  cartService = new CartServiceImpl();

    protected void FurnPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String keyWord = req.getParameter("keyWord");
        //参数keyWord有参数,没有keyWord则返回所有的家居
        if (keyWord == null){
            keyWord = "";
        }
        StringBuilder url = new StringBuilder("&keyWord=");
        // 这个是同一个页面才进行操作，我这不是同一个页面
        if (!"".equals(keyWord)){
            url.append(keyWord);
        }
        HttpSession session = req.getSession();
        String pageNo = req.getParameter("PageNo");
        if (pageNo == null){
            pageNo = "1";
        }
        Page<Furn> search = furnService.Search(keyWord, Tool.getNumber(pageNo, 1), MAXPAGESIZE);
        search.setUrl(new String(url));
        session.setAttribute("keyWord",keyWord);
        session.setAttribute("FurnPage",search);
        //剩下的是单独处理购物车
        User user = (User) session.getAttribute("User");
        if (user == null){
            search.setCartAmount(0);
        }else {
            search.setCartAmount(cartService.CartIdNumber(user.getId()));
        }
        
        resp.sendRedirect("/Web/views/customer/FurnSearch.jsp");
    }


}
